<template>
  <div class="consultation">
    <h2>乘坐人管理</h2>
    <div class="consultation-block">
      <div class="table" v-loading="tableLoading">
        <el-table size="mini" :data="tableData" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="realname" label="用户名"> </el-table-column>
          <el-table-column prop="cardtype" label="证件类型" :formatter="format1"> </el-table-column>
          <el-table-column prop="cardno" label="证件号码"> </el-table-column>
          <el-table-column prop="pType" label="旅客类型" :formatter="format2"> </el-table-column>
          <el-table-column prop="examine" label="是否认证" :formatter="format3"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                v-if="scope.row.examine === 'false' && (scope.row.pType !== '0' || scope.row.pType !== '1') && scope.row.pics !== '[]'"
                @click="openDialog('edit', scope.row)"
                >审核</el-button
              >
              <el-button
                type="danger"
                size="mini"
                v-if="scope.row.username !== 'admin'"
                @click="deleteData(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="currentPage"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="isVisible"
      width="30%"
      :before-close="handleClose"
    > 
      <h3>点击查看更多大图</h3>
      <el-image 
        v-if="pics.length"
        style="width: 200px; height: 200px"
        :src="pics[0]" 
        :preview-src-list="pics">
      </el-image>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('true')">审核通过</el-button>
        <el-button type="danger" @click="submitForm('fail')">审核不通过</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment';
import { getPass, deletePass, updatePass } from "../service.js";
export default {
  name: "home",
  mounted() {
    this.refreshTable();
  },
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      isVisible: false,
      dialogTitle: "",
      form: {},
      pics: []
    };
  },
  methods: {
    openDialog(type, row = {}) {
      this.form = row
      this.dialogTitle = "审核";
      this.pics = JSON.parse(row.pics)
      this.pics.forEach((v, i) => {
        this.pics[i] = `http://127.0.0.1:3000/${v}`
      })
      this.isVisible = true;
    },
    handleClose() {
      this.isVisible = false;
    },
    handleCurrentChange(index) {
      this.currentPage = index
      this.refreshTable()
    },
    submitForm(examine) {
      updatePass(this.form.id, {
        examine
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success("操作成功");
          this.refreshTable();
          this.isVisible = false;
        }
      });
    },
    addData() {
      this.dialogTitle = "新增";
      this.isVisible = true;
    },
    refreshTable() {
      getPass({
        page: this.currentPage,
        pageSize: this.pageSize
      }).then((res) => {
        this.tableData = res.code === 200 ? res.data.data : [];
        this.total = res.data.total
      });
    },
    deleteData(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletePass(row.id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.refreshTable();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    format1(row) {
      switch (row.cardtype) {
        case '0':
          return '中国居民身份证';
        case '1':
          return '港澳台居民来往内地通行证'
        case '2':
          return '台湾居民来往大陆通行证'
        case '3':
          return '护照'
      }
    },
    format2(row) {
      switch (row.pType) {
        case '0':
          return '成人';
        case '1':
          return '儿童'
        case '2':
          return '学生'
        case '3':
          return '残军'
      }
    },
    format3(row) {
      if (row.examine === 'true') {
        return '已认证'
      }
      if (row.examine === 'fail') {
        return '认证不通过'
      }
      if (row.pType !== '0') {
        if (JSON.parse(row.pics).length === 0) {
          return '未上传证件'
        } else {
          return '未认证'
        }
      }
    }
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 60px;
  height: 60px;
  line-height: 60px;
  border: 1px dashed gray;
  text-align: center;
}
.avatar {
  width: 60px;
  height: 60px;
  display: block;
}
.img-blocks {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.el-icon-circle-close {
  position: absolute;
  top: -5px;
  right: -5px;
  cursor: pointer;
  color: red;
}
</style>
